package ntut.andengine.game;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel17 extends GameLevel {

	PhysicalComponent bar1;
    PhysicalComponent bar2;
    PhysicalComponent bar3;
    PhysicalComponent bar4;
	GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(195, 40));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel17.this, GameLevel3.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        PhysicalFactory.CreateWall(this, new Vector2(100, 220), 20, 440, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(190, 210), 380, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(100, 150), 20, 300, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(200, 310), 400, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(240, 450), 480, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(300, 310), 380, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(390, 460), 20, 280, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(390, 400), 20, 800, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(280, 610), 240, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(140, 450), 280, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(240, 610), 480, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(170, 530), 20, 140, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(10, 530), 20, 140, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(100, 710), 20, 180, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(240, 719), 260, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(290, 60), 20, 120, ComponentColor.Red);
        bar1 = PhysicalFactory.CreateBar(this, new Vector2(195, 110), ComponentColor.Red, new Vector2(0, 0), false);
        bar2 = PhysicalFactory.CreateBar(this, new Vector2(195, 110), ComponentColor.Blue, new Vector2(0, 0), false);
        bar3 = PhysicalFactory.CreateBar(this, new Vector2(470, 200), ComponentColor.Red, new Vector2(-20, 0), true);
        bar4 = PhysicalFactory.CreateBar(this, new Vector2(470, 200), ComponentColor.Blue, new Vector2(-20, 0), true);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(47, 60), ComponentColor.Red);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	
		mScene.registerUpdateHandler(new IUpdateHandler(){
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
				CheckPutBack();
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});

		return mScene;
	}
	
	private void CheckPutBack() {
      
        if(bar3.getPosition().x < 290 || bar3.getPosition().x > 470){
        	if(bar3.getPosition().x > 470 ){
            	bar3.setPosition(new Vector2(470, bar3.getPosition().y));
            }
            if(bar3.getPosition().x < 290 ){
            	bar3.setPosition(new Vector2(290, bar3.getPosition().y));
            }
            bar3.setVelocity(bar3.getVelocity().mul(-1));
        }
        if(bar4.getPosition().x < 290 || bar4.getPosition().x > 470){
        	if(bar4.getPosition().x > 470 ){
            	bar4.setPosition(new Vector2(470, bar4.getPosition().y));
            }
            if(bar4.getPosition().x < 290 ){
            	bar4.setPosition(new Vector2(290, bar4.getPosition().y));
            }
            bar4.setVelocity(bar4.getVelocity().mul(-1));
        }
    }
}
